﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using BWAP.Filters;
using Model;
using Tools;
namespace BWAP.Controllers
{
    public class BooksManageController : Controller
    {
        //
        // GET: /BooksManage/
        [CheckLogin]
        #region 图书类别
        public ActionResult BookType()
        {
            ViewBag.BookTypeList = ManageBooksBLL.BookTypeList();
            return View();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult RemoveBookType(int id = 0)
        {
            ManageBooksBLL.RemoveBookType(id);
            return null;
        }

        /// <summary>
        /// 跳转到添加页面
        /// </summary>
        /// <returns></returns>
        public ActionResult AddBookType()
        {
            return View();
        }
        /// <summary>
        /// 提交表单添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddBookType(BookType item)
        {
            return Content(ManageBooksBLL.AddBookType(item));
        }
        /// <summary>
        /// 跳转到修改页面
        /// </summary>
        /// <param name="id">类型id</param>
        /// <returns></returns>
        public ActionResult EditBookType(int id = 0)
        {
            List<BookType> TypeList = ManageBooksBLL.BookTypeList();
            ViewBag.BookType = TypeList.Where(a => a.TypeID == id).SingleOrDefault();
            return View();
        }
        [HttpPost]
        public ActionResult EditBookType(BookType item)
        {
            return Content(ManageBooksBLL.EditBookType(item));
        }
        #endregion

        #region 图书信息

        /// <summary>
        /// 跳转到图书信息
        /// </summary>
        /// <returns></returns>
        public ActionResult BooksInfo()
        {
            ViewBag.BookInfoList = ManageBooksBLL.BookInfoList();
            return View();
        }
        /// <summary>
        /// 删除图书
        /// </summary>
        /// <param name="id">图书id</param>
        /// <returns></returns>
        public ActionResult RemoveBookInfo(int id = 0)
        {
            ManageBooksBLL.RemoveBookInfo(id);
            return null;
        }
        /// <summary>
        /// 添加图书页面
        /// </summary>
        /// <returns></returns>
        public ActionResult AddBookInfo()
        {
            ViewBag.BookTypeList = ManageBooksBLL.BookTypeList();
            return View();
        }
        [HttpPost]
        public ActionResult AddBookInfo(BookInfo item)
        {

            return Content(ManageBooksBLL.AddBookInfo(item));
        }
        /// <summary>
        /// 修改图书页面
        /// </summary>
        /// <returns></returns>
        public ActionResult EditBookInfo(int id = 0)
        {
            ViewBag.BookTypeList = ManageBooksBLL.BookTypeList();
            ViewBag.BookInfo = ManageBooksBLL.BookInfoList().Where(a => a.BookID == id).SingleOrDefault();
            return View();
        }
        [HttpPost]
        public ActionResult EditBookInfo(BookInfo item)
        {

            return Content(ManageBooksBLL.EditBookInfo(item));
        }

        [HttpPost]
        public ActionResult AddBookPic(HttpPostedFileBase Pic,int id = 0)
        {
            if (Pic != null)
            {
                string name = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
                string fileName = Pic.FileName;
                string exName = System.IO.Path.GetExtension(fileName);
                fileName = name + exName;
                string savaPath = Server.MapPath(string.Format("~/Images/{0}", fileName));
                Pic.SaveAs(savaPath);
                ViewBag.BookInfoList = ManageBooksBLL.BookInfoList();
                ManageBooksBLL.AddBookPic(fileName,id);
                return View("BooksInfo");
            }
            else
            {
                return Content("上传失败,无文件");
            }
            
        }
        #endregion

        #region 图书评论
        public ActionResult BookComments()
        {
            ViewBag.BookComList = ManageBooksBLL.BookComList();
            return View();
        }

        #endregion
    }
}
